import 'dart:math';
import 'package:dio/dio.dart';
import 'package:commons/commons.dart';

class NetRequestInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    String ctimestamp =
        (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
    String cnonce = randomString(6);

    var user = UserController().state.user;
    var headers = {
      "appCode": user?.appCode ?? "",
      "userId": user?.userId ?? '-1',
      "token": user?.token ?? '',
      "userName":
          user?.userName?.isNotEmpty != true
              ? ''
              : Uri.encodeComponent(user!.userName.toString()),
      "organId": user?.loginPostInfo!.orgId ?? '',
      "tenantId": user?.tenantId! ?? '',
      "type": 1,
      "ctimestamp": ctimestamp,
      "cnonce": cnonce,
      // "csign": csign  VeraWong_ww
    };
    options.headers.addAll(headers);

    return handler.next(options); //continue
  }

  String randomString(int len) {
    String charsAll = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = charsAll.length;
    var pwd = '';
    for (int i = 0; i < len; i++) {
      pwd += charsAll[Random().nextInt(maxPos)];
    }
    return pwd;
  }
}
